/**
*
* @file
*		heap_test_av
*
* @brief
*		Tests for heap by anyav@
*/
#include <algorithm>
#include <functional>
#include <Windows.h>
#include <process.h>
#include <stdlib.h>
#include <stdio.h>
#include "../../api/heap.h"
#include "../../src/vasyarv/heap_vasya.h"
#include <tchar.h>
#include <time.h>
#include <iostream>
#include <typeinfo>
#include <exception>
 #include <cstdlib>
/**
 * abstract test
 */
 using namespace std;

DWORD ThreadProc (LPVOID lpdwThreadParam );

DWORD WINAPI threadtest( LPVOID lpParam )
{

    heap_vasya* mttest = (heap_vasya*)lpParam;
    (int*)mttest->alloc(16,0);
    printf("Thr created\n");
    return 0;

}


int test( heap876* hp )
{
    ///test for capacity
    int i = 0;
    int step = 1024;
    void *test_capacity;
    int num_step = hp->get_volume()/step; ///we will allocate memory via such blocks
    for (i=0;i<num_step;i+=step) {
        test_capacity = hp->alloc(16);
        hp->free(test_capacity);
    }

    int threads_n = 2;
    HANDLE threads[threads_n];
    try
	{
		heap_vasya hp( 128 * 128 * 16, heap876::alloc_simple );
		for (int j = 0; j < threads_n; j++)
		{
			threads[j] = CreateThread(0, 0, (LPTHREAD_START_ROUTINE)(threadtest), &hp, 0, 0);
		}
		WaitForMultipleObjects(threads_n, threads, TRUE, INFINITE);
	} catch (exception& e) {
		cout << e.what();
	}



    printf("heap<%s> volume is %p. Occupancy is %p\n", hp->get_name(), hp->get_volume(), hp->get_occupancy());
	return 0;
}

/**
 * execution of test above your class
 */
int main( int argc, char* argv[] )
{

    //InitializeCriticalSection(&cs);
    ///Thread test


	heap_vasya hp_vasya( 2056, heap876::alloc_simple );
	return test( &hp_vasya );



}
